from django.contrib.auth.backends import ModelBackend
import re
from .models import User

def get_user_by_account(account):
    try:
        if re.match('^1[3-9\d]{9}$',account):
            user = User.objects.get(mobile=account)
        else:
            user = User.objects.get(username=account)
    except Exception as e:
        return None
    else:
        return user


class UsernameMobileAuthBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        try:
            user = User.objects.get(username=username)
        except Exception as e:
            try:
                user = User.objects.get(mobile=username)
            except Exception as e:
                return None

        if not user.check_password(password):
            return None
        return user